﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;
using System.Threading;
using System.Reflection;

namespace Fares
{
    public partial class ArticoleCardFrm : FrmWorkspace
    {
        private String _denumire = String.Empty;
        DataSets.ESRPDataSetArticol.FaresSetupRow FSRow = null;
        public String DenumireArticol
        {
            set
            {
                _denumire = value;
            }
        }
        private String _codArticol = String.Empty;
        public String CodArticol
        {
            set
            {
                _codArticol = value;
            }
        }
        private String ContArticolImplicit = String.Empty;
        private String ContArticolCheltuialaImplicit = String.Empty;
        private String ContVenitCheltuialaImplicit = String.Empty;
        private Decimal CotaTVAImplicita = Decimal.Zero;
        private Int32 CodUMImplicit = 0;
        private Boolean FolosesteVarianta = false;
        public Int32 idArtSel = -1;
        public Boolean Bugetara = false;
        public Boolean Productie = false;
        private Boolean ParteneriProprii = false;
        private Int64 IDPartener = -1;
        private Boolean GestiuneCantitativa = false;

        public ArticoleCardFrm(ModuleClass mc,Int32 _idArt,Int64 _idPart = -1):base(mc)
        {
            InitializeComponent();
            idArtSel = _idArt;
            workConnString = mc.workConnectionString;
            IDPartener = _idPart;
            esrpDataSetArticol.Articole.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            esrpDataSetArticol.Articole.GreutateColumn.DefaultValue = 0;
            esrpDataSetArticol.Articole.EsteServiciuColumn.DefaultValue = false;
            esrpDataSetArticol.Articole.PretVanzareColumn.DefaultValue = 0;
            esrpDataSetArticol.Articole.MateriePrimaColumn.DefaultValue = 0;
            esrpDataSetArticol.Articole.PretVanzareValutaColumn.DefaultValue = 0;
            esrpDataSetArticol.Articole.LungimeColumn.DefaultValue = 0;
            esrpDataSetArticol.Articole.LatimeColumn.DefaultValue = 0;
            esrpDataSetArticol.Articole.GrosimeColumn.DefaultValue = 0;
           

            esrpDataSetArticol.Stocuri.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            esrpDataSetArticol.Stocuri.StocInitialColumn.DefaultValue = 0;
            esrpDataSetArticol.Stocuri.StocPrecedentColumn.DefaultValue = 0;
            esrpDataSetArticol.Stocuri.IntrariColumn.DefaultValue = 0;
            esrpDataSetArticol.Stocuri.IesiriColumn.DefaultValue = 0;
            //esrpDataSetArticol.Stocuri.IDDepartamentColumn.DefaultValue = 0;
            esrpDataSetArticol.UMConversie.FactorColumn.DefaultValue = 0;

            esrpDataSetArticol.ProductieOperatiuni.PozitieColumn.DefaultValue = 1;
            esrpDataSetArticol.ProductieOperatiuni.ActivColumn.DefaultValue = true;
            esrpDataSetArticol.ProductieOperatiuni.NrPersoaneColumn.DefaultValue = 1;
            esrpDataSetArticol.ProductieOperatiuni.UtilajColumn.DefaultValue = String.Empty;

            esrpDataSetArticol.ProductieArticoleOperatiuni.IntrareColumn.DefaultValue = true;
            esrpDataSetArticol.ProductieArticoleOperatiuni.MultiplicareColumn.DefaultValue = 1;


            articoleTableAdapter.Adapter.RowUpdated += new SqlRowUpdatedEventHandler(Adapter_RowUpdated);
            productieOperatiuniTableAdapter.Adapter.RowUpdated += new SqlRowUpdatedEventHandler(AdapterProductie_RowUpdated);

            HandleRezumat += new EventHandler(ArticoleCardFrm_HandleRezumat);
            EnableMainBrowseButton = true;
            lstPK.Add(denumireTB);

            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.Add(global::ERPStudio.Properties.Resources._001_35);
            this.imageList1.Images.Add(global::ERPStudio.Properties.Resources._001_33);
            this.imageList1.Images.Add(global::ERPStudio.Properties.Resources._001_08);
            this.imageList1.Images.Add(global::ERPStudio.Properties.Resources._001_48);

            articolePreturiDGV.m_AutoCompletionColumns.Add(partenerColumn);
            esrpDataSetArticol.ArticolePreturi.DataAplicareColumn.DefaultValue = DateTime.Now;
            if (IDPartener!=-1)
                esrpDataSetArticol.ArticolePreturi.IDPartenerColumn.DefaultValue = IDPartener;
        }

        void ArticoleCardFrm_HandleRezumat(object sender, EventArgs e)
        {
            Int32 selID = -1;
            if (articoleBindingSource.Current != null)
            {
                DataSets.ESRPDataSetArticol.ArticoleRow prr = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;
                selID = (Int32)prr.IDArticol;
            }

            ArticoleRezumatFrm frm = new ArticoleRezumatFrm(ModuleInfo, selID);
            frm.SelectionAddOn = true;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (frm.selectedDataRow != null)
                {
                    articoleBindingSource.RemoveFilter();
                    DataSets.ESRPDataSetArticol.ArticoleRow pr = frm.selectedDataRow.Row as DataSets.ESRPDataSetArticol.ArticoleRow;
                    articoleBindingSource.Position = articoleBindingSource.Find("IDArticol", pr.IDArticol);
                }
            }
            EnableMainBrowseButton = true;
        }

        void Adapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            DataSets.ESRPDataSetArticol.ArticoleRow ar = e.Row as DataSets.ESRPDataSetArticol.ArticoleRow;
            if (e.Errors != null)
                throw e.Errors;
            if (e.StatementType == StatementType.Insert)
            {
                DataRow[] rdc = esrpDataSetArticol.Stocuri.Select("IDArticol=" + e.Row["IDArticol", DataRowVersion.Original].ToString(),
                        "IDArticol", DataViewRowState.Added);
                foreach (DataRow row in rdc)
                {
                    row["IDArticol"] = e.Row["IDArticol"];
                }
                DataRow[] rdc2 = esrpDataSetArticol.UMConversie.Select("IDArticol=" + e.Row["IDArticol", DataRowVersion.Original].ToString(),
                        "IDArticol", DataViewRowState.Added);
                foreach (DataRow row in rdc2)
                {
                    row["IDArticol"] = e.Row["IDArticol"];
                }
            }
        }

        void AdapterProductie_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            DataSets.ESRPDataSetArticol.ArticoleRow ar = e.Row as DataSets.ESRPDataSetArticol.ArticoleRow;
            if (e.Errors != null)
                throw e.Errors;
            if (e.StatementType == StatementType.Insert)
            {
                DataRow[] rdc = esrpDataSetArticol.ProductieArticoleOperatiuni.Select("IDOperatiune=" + e.Row["IDOperatiune", DataRowVersion.Original].ToString(),
                        "IDOperatiune", DataViewRowState.Added);
                foreach (DataRow row in rdc)
                {
                    row["IDOperatiune"] = e.Row["IDOperatiune"];
                }
            }
        }

        private void CoduriArticoleParteneri(object sender, EventArgs e)
        {
            if (articoleBindingSource.Current == null)
                return;
            if (((DataRowView)articoleBindingSource.Current).IsNew)
            {
                MessageBox.Show("Va rugam sa salvati articolul inaite de a seta codurile pentru parteneri!",
                    "Eroare",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Hand);
                return;
            
            }
            DataSets.ESRPDataSetArticol.ArticoleRow ra = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;
            ArticoleParteneriFrm frm = new ArticoleParteneriFrm(ModuleInfo,FolosesteVarianta);
            frm.IdArticol = ra.IDArticol;
            frm.MdiParent = MdiParent;
            frm.Show();
        }

        private void VarianteArticole(object sender, EventArgs e)
        {
            if (articoleBindingSource.Current == null)
                return;
            if (((DataRowView)articoleBindingSource.Current).IsNew)
            {
                MessageBox.Show("Va rugam sa salvati articolul inaite de a seta codurile pentru parteneri!",
                    "Eroare",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Hand);
                return;

            }
            DataSets.ESRPDataSetArticol.ArticoleRow ra = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;
            VarianteArticoleFrm frm = new VarianteArticoleFrm(ModuleInfo, ra.IDArticol);
            //frm.MdiParent = MdiParent;
            frm.ShowDialog();
            SqlConnection con = DeschideConexiune(workConnString);
            articoleVarianteTableAdapter.Connection = con;
            articoleVarianteTableAdapter.Fill(esrpDataSetArticol.ArticoleVariante);
            InchideConexiune(con);
        }

        private void ArticoleCardFrm_Load(object sender, EventArgs e)
        {
                                
            AddButton("Coduri Art.", global::ERPStudio.Properties.Resources._001_18, CoduriArticoleParteneri, ToolStripItemAlignment.Right, false);
            AddButton("Variante", global::ERPStudio.Properties.Resources._001_35, VarianteArticole, ToolStripItemAlignment.Right, false);
            AddBindingSource(articoleBindingSource);
            AddBindingSource(fKStocuriArticoleBindingSource);
            AddBindingSource(fkUMConversieBindingSource);
            AddBindingSource(productieOperatiuniBindingSource);
            AddBindingSource(productieArticoleOperatiuniBindingSource);
            AddBindingSource(articolePreturiBindingSource);


            /// adauga tabelele la lista de adaptoare pentru salvarea in baza de date!
            /// 
            listTableAdaptor.Add(articoleTableAdapter.Adapter);
            listTableAdaptor.Add(stocuriTableAdapter.Adapter);
            listTableAdaptor.Add(uMConversieTableAdapter.Adapter);
            listTableAdaptor.Add(productieOperatiuniTableAdapter.Adapter);
            listTableAdaptor.Add(productieArticoleOperatiuniTableAdapter.Adapter);
            listTableAdaptor.Add(articolePreturiTableAdapter.Adapter);

           

            Setup();
            esrpDataSetArticol.Articole.ContColumn.DefaultValue = ContArticolImplicit;
            esrpDataSetArticol.Articole.ContCheltuialaColumn.DefaultValue = ContArticolCheltuialaImplicit;
            esrpDataSetArticol.Articole.ContVenitColumn.DefaultValue = ContVenitCheltuialaImplicit;
            try
            {
                SqlConnection con = DeschideConexiune(workConnString);
                this.faresSetupTableAdapter.Connection = con;
                this.faresSetupTableAdapter.Fill(this.esrpDataSetArticol.FaresSetup,ModuleInfo.CodUnitate);
                if (faresSetupBindingSource.Current!=null)
                    FSRow = ((DataRowView)faresSetupBindingSource.Current).Row as DataSets.ESRPDataSetArticol.FaresSetupRow;


                this.fares_ParteneriCUITableAdapter.Connection = con;
                if (ParteneriProprii)
                    this.fares_ParteneriCUITableAdapter.FillByUnitate(esrpDataSetArticol.fares_ParteneriCUI, ModuleInfo.CodUnitate);
                else
                    this.fares_ParteneriCUITableAdapter.Fill(esrpDataSetArticol.fares_ParteneriCUI);
                this.articolePreturiTableAdapter.Connection = con;
                this.articolePreturiTableAdapter.Fill(this.esrpDataSetArticol.ArticolePreturi);

                if (Productie)
                {

                    //this.fares_viewArticolProductieTableAdapter.Connection = con;
                    //this.fares_viewArticolProductieTableAdapter.Fill(this.esrpDataSetArticol.fares_viewArticolProductie, ModuleInfo.CodUnitate);

                    this.productieArticoleOperatiuniTableAdapter.Connection = con;
                    this.productieArticoleOperatiuniTableAdapter.Fill(this.esrpDataSetArticol.ProductieArticoleOperatiuni);
                    this.productieOperatiuniTableAdapter.Connection = con;
                    this.productieOperatiuniTableAdapter.Fill(this.esrpDataSetArticol.ProductieOperatiuni);
                }

                this.articoleAliniateTableAdapter.Connection = con;
                this.articoleAliniateTableAdapter.FillByUnitate(this.esrpDataSetArticol.ArticoleAliniate, ModuleInfo.CodUnitate);
                this.valutaTableAdapter.Connection = con;
                this.valutaTableAdapter.Fill(this.esrpDataSetArticol.Valuta);


                this.uMConversieTableAdapter.Connection = con;
                this.uMConversieTableAdapter.Fill(this.esrpDataSetArticol.UMConversie);

                articoleVarianteTableAdapter.Connection = con;
                articoleVarianteTableAdapter.Fill(esrpDataSetArticol.ArticoleVariante);
                this.departamenteTableAdapter.Connection = con;
                this.departamenteTableAdapter.FillBy(this.esrpDataSetArticol.Departamente, ModuleInfo.CodUnitate);


                if (FSRow.FolosesteCPV)
                {
                    //this.cPVTableAdapter.Connection = con;
                    //this.cPVTableAdapter.Fill(this.esrpDataSetArticol.CPV);
                    //CPVP.Visible = false;
                }



                this.conturiTableAdapter.Connection = con;
                this.conturiTableAdapter.Fill(this.esrpDataSetArticol.Conturi, ModuleInfo.CodUnitate);

                this.unitatiMasuraTableAdapter.Connection = con;
                this.unitatiMasuraTableAdapter.Fill(this.esrpDataSetArticol.UnitatiMasura);

                this.tipCategoriiArticoleTableAdapter.Connection = con;
                this.tipCategoriiArticoleTableAdapter.Fill(this.esrpDataSetArticol.TipCategoriiArticole);
                this.stocuriTableAdapter.Connection = con;
                this.stocuriTableAdapter.Fill(this.esrpDataSetArticol.Stocuri);

                this.articoleTableAdapter.Connection = con;
                this.articoleTableAdapter.Fill(this.esrpDataSetArticol.Articole, ModuleInfo.CodUnitate);
                if (_denumire != String.Empty || _codArticol != String.Empty)
                {
                    if (_denumire != String.Empty)
                    {
                        esrpDataSetArticol.Articole.DenumireColumn.DefaultValue = _denumire.ToUpper();
                        if (_codArticol == String.Empty)
                        {
                            SqlCommand cmd = new SqlCommand("fares_GenereazaCodArticol", con);
                            cmd.CommandType = CommandType.StoredProcedure;
                            String radacina = String.Empty;
                            if (_denumire.Length > 2)
                                radacina = _denumire.Substring(0, 2);
                            else
                                radacina = _denumire;
                            cmd.Parameters.Add(new SqlParameter("@radacina", radacina));
                            cmd.Parameters.Add(new SqlParameter("@codNou", ""));
                            cmd.Parameters[1].Direction = ParameterDirection.Output;
                            cmd.Parameters[1].DbType = DbType.String;
                            cmd.Parameters[1].Size = 20;
                            cmd.ExecuteScalar();
                            esrpDataSetArticol.Articole.CodArticolColumn.DefaultValue = cmd.Parameters[1].Value.ToString().ToUpper();
                        }
                    }
                    if (_codArticol != String.Empty)
                        esrpDataSetArticol.Articole.CodArticolColumn.DefaultValue = _codArticol.ToUpper();
                    Adauga(this, null);
                }
                InchideConexiune(con);
            }
            catch(Exception ex)
            {
                TrateazaExceptie(ex);
            }


            if (articoleBindingSource.Count == 0)
            {
                Adauga(this, null);
                //DataSets.ESRPDataSetArticol.ArticoleRow rd = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;

            }
            else
            {
                if (idArtSel != -1)
                {
                    int pos = articoleBindingSource.Find("IDArticol", idArtSel);
                    if (pos != -1)
                        articoleBindingSource.Position = pos;
                }
            }
            HandleSalvare += Salvare;
            ActiveControl = codArticolTB;
            conturiBindingSource.Sort = "SortValue";
            conturiBindingSourceCheltuiala.Sort = "SortValue";
            conturiBindingSourceVenituri.Sort = "SortValue";

            SetTitle();
        }

        private void Setup()
        {
            SqlConnection wConn = DeschideConexiune(workConnString);
            Object ret = GetSetupValue("FaresSetup", "ContArticolImplicit", ModuleInfo.CodUnitate, wConn);
            String val = String.Empty;
            if (ret != null && ret != DBNull.Value)
                val = ret.ToString();
            else
            {
                while (ret == null)
                {
                    MessageBox.Show("Modulul Parteneri nu a fost configurat. \nVa rugam sa-l configurati!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                    FaresSetupFrm frm = new FaresSetupFrm(this.ModuleInfo);
                    frm.ShowDialog();
                    ret = GetSetupValue("FaresSetup", "ContArticolImplicit", ModuleInfo.CodUnitate, wConn);
                }
            }

            ContArticolImplicit = val;

            ret = GetSetupValue("FaresSetup", "ContCheltuialaArticolImplicit", ModuleInfo.CodUnitate, wConn);
            val = String.Empty;
            if (ret != DBNull.Value && ret != null)
                val = ret.ToString();

            ContArticolCheltuialaImplicit = val;

            ret = GetSetupValue("FaresSetup", "ContVenitArticolImplicit", ModuleInfo.CodUnitate, wConn);
            val = String.Empty;
            if (ret != DBNull.Value && ret!=null)
                val = ret.ToString();

            ContVenitCheltuialaImplicit = val;

            ret = GetSetupValue("FaresSetup", "FolosesteVarinata", ModuleInfo.CodUnitate, wConn);
            if (ret != DBNull.Value && ret != null)
                FolosesteVarianta = Convert.ToBoolean(ret);
            variantaColumn.Visible = FolosesteVarianta;

            ret = GetSetupValue("FaresSetup", "CotaTVAImplicita", ModuleInfo.CodUnitate, wConn);
            Decimal vald = Decimal.Zero;
            if (ret != DBNull.Value && ret != null)
                vald = Convert.ToDecimal(ret);

            ret = GetSetupValue("FaresSetup", "DenumireNumeric1", ModuleInfo.CodUnitate, wConn);
            lungimeLabel.Text = ret.ToString();
            ret = GetSetupValue("FaresSetup", "DenumireNumeric2", ModuleInfo.CodUnitate, wConn);
            latimeLabel.Text = ret.ToString();
            ret = GetSetupValue("FaresSetup", "DenumireNumeric3", ModuleInfo.CodUnitate, wConn);
            grosimeLabel.Text = ret.ToString();


            ret = GetSetupValue("FaresSetup", "GestiuneCantitativa", ModuleInfo.CodUnitate, wConn);
            GestiuneCantitativa = Convert.ToBoolean(ret.ToString());


            CotaTVAImplicita = vald;
            esrpDataSetArticol.Articole.CotaTVAColumn.DefaultValue = CotaTVAImplicita;

            ret = GetSetupValue("FaresSetup", "ImplementareAmbalaje", ModuleInfo.CodUnitate, wConn);
            Boolean valb = false;
            if (ret != DBNull.Value && ret != null)
                valb = Convert.ToBoolean(ret);

            SuprafataL.Visible = valb;
            SupTotalaL.Visible = valb;
            SupTotalNUD.Visible = valb;

            ret = GetSetupValue("FaresSetup", "ParteneriProprii", ModuleInfo.CodUnitate, wConn);
            if (ret != DBNull.Value && ret != null)
                ParteneriProprii = Convert.ToBoolean(ret);

            ret = GetSetupValue("FaresSetup", "ArtAlImplicit", ModuleInfo.CodUnitate, wConn);
            String valartal = String.Empty;
            if (ret != DBNull.Value && ret != null)
                valartal = ret.ToString();
            else
            {
                while (ret == null)
                {
                    MessageBox.Show("Modulul Parteneri nu a fost configurat. \nVa rugam sa setati articolul si alineatul implicit!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                    FaresSetupFrm frm = new FaresSetupFrm(this.ModuleInfo);
                    frm.ShowDialog();
                    ret = GetSetupValue("FaresSetup", "ArtAlImplicit", ModuleInfo.CodUnitate, wConn);
                    valartal = ret.ToString();
                }
            }
            if (valartal!=String.Empty)
                esrpDataSetArticol.Articole.ArtAlColumn.DefaultValue = valartal;

            ret = GetSetupValue("FaresSetup", "ValutaImplicita", ModuleInfo.CodUnitate, wConn);
            Int16 valval = 0;
            if (ret != DBNull.Value && ret != null)
                valval = Convert.ToInt16(ret);
            else
            {
                while (ret == null)
                {
                    MessageBox.Show("Modulul Parteneri nu a fost configurat. \nVa rugam sa setati valuta implicit!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                    FaresSetupFrm frm = new FaresSetupFrm(this.ModuleInfo);
                    frm.ShowDialog();
                    ret = GetSetupValue("FaresSetup", "ValutaImplicita", ModuleInfo.CodUnitate, wConn);
                    valval = Convert.ToInt16(ret);
                }
            }
            esrpDataSetArticol.Articole.PretVanzareCodValutaColumn.DefaultValue = valval;
            esrpDataSetArticol.ArticolePreturi.CodValutaColumn.DefaultValue = valval;


            SqlCommand cmd = new SqlCommand("select TOP 1 [IDUnitateMasura] from [UnitatiMasura] where Compusa=0", wConn);
            ret = cmd.ExecuteScalar();
            if (ret != null)
            {
                CodUMImplicit = Convert.ToInt32(ret);
            }
            esrpDataSetArticol.Articole.IDUnitateMasuraColumn.DefaultValue = CodUMImplicit;

            //cmd.CommandText="select Count(*) from [Departamente] where [CodUnitate]=" +ModuleInfo.CodUnitate.ToString();
            //ret = cmd.ExecuteScalar();
            //if (ret != null)
            //{
            //    FolosesteDepartamente = Convert.ToInt32(ret) != 0;
            //    DepartamentColumn.Visible = FolosesteDepartamente;
            //}

            DepartamentColumn.Visible = false;

            ret = GetSetupValue("FaresSetup", "Bugetara", ModuleInfo.CodUnitate, wConn);
            if (ret != null && ret != DBNull.Value)
                Bugetara = Convert.ToBoolean(ret);
            
            artAlL.Visible = Bugetara;
            artAlCB.Visible = Bugetara;

            #region determina categoria implicita
            SqlCommand comm = new SqlCommand(String.Empty, wConn);
            comm.CommandText = "SELECT MIN([IDCategorie]) FROM [TipCategoriiArticole]";
            comm.CommandType = CommandType.Text;
            Object retu = comm.ExecuteScalar();
            Int32 idRegFis = 0;
            if (retu != null && retu != DBNull.Value)
                idRegFis = Convert.ToInt32(retu);
            esrpDataSetArticol.Articole.IDCategorieColumn.DefaultValue = idRegFis;
            #endregion

            ret = GetSetupValue("FaresSetup", "Productie", ModuleInfo.CodUnitate, wConn);
            if (ret != null && ret != DBNull.Value)
                Productie = Convert.ToBoolean(ret);

            if(!Productie)
                tabControl1.TabPages.Remove(ProductieTP);
            InchideConexiune(wConn);
            unitatiMasuraSimpleBindingSource.Filter = "Compusa=0";


            


            /*Object retObj = GetSetupValue("FaresSetup", "LunaCurenta", ModuleInfo.CodUnitate, wConn);
            if (retObj != null && retObj != DBNull.Value)
                LunaCurenta = Convert.ToInt32(retObj);

            retObj = GetSetupValue("FaresSetup", "AnCurent", ModuleInfo.CodUnitate, wConn);
            if (retObj != null && retObj != DBNull.Value)
                AnulCurent = Convert.ToInt32(retObj);
            
            
            if (LunaCurenta == 0)
            {
                MessageBox.Show("Modulul Parteneri nu a fost configurat corect (Luna curenta=0). \nVa rugam sa-l configurati!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                return;
            }

            ///daca este in perioada curenta
            if (dscModule.AnArhiva == String.Empty)
            {
                documenteBindingSource.Filter = "[DataDoc] <= '" + dataDocDTP.MaxDate.Year.ToString() + "-" + dataDocDTP.MaxDate.Month.ToString() + "-" + dataDocDTP.MaxDate.Day.ToString() + "'";
                dataDocDTP.MaxDate = new DateTime(AnulCurent, LunaCurenta, 1).AddMonths(1).AddDays(-1);
                esrpDataSetArticol.Documente.DataDocColumn.DefaultValue = dataDocDTP.MaxDate;
                SetInfoLunaAnul(LunaCurenta.ToString() + "," + AnulCurent.ToString());
            }
            */

            pretColumn.Visible = !GestiuneCantitativa;
            pretValutaColumn.Visible = !GestiuneCantitativa;
            valutaColumn.Visible = !GestiuneCantitativa;

        }

        private void iDUnitateMasuraCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            articoleBindingSource_PositionChanged(sender, e);
        }

        override public void Salvare(object sender, EventArgs args)
        {
            if ((articoleBindingSource.Current == null))
                return;

            if (factorNUD.Visible && factorNUD.Value == Decimal.Zero)
            {
                MessageBox.Show("Factorul de conversie nu poate sa fie zero!",
                        "Eroare",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Hand);
                
                ActiveControl = factorNUD;
                return;
            }

            if ((iDUnitateMasuraCB.SelectedItem == null))
            {
                MessageBox.Show("Unitatea de masura este informatie obligatorie!",
                        "Eroare",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Hand);
                return;
            }
            if (!SalvareLocal())
                return;


            #region Verificare conturi
            DataSets.ESRPDataSetArticol.ArticoleRow ar = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;

            if (ar != null && (ar.IsContVenitNull() || ar.ContCheltuiala == String.Empty || ar.Cont == String.Empty || ar.ContVenit == String.Empty))
            {
                MessageBox.Show("Contul de cheltuiala, venit si contul de articol sunt campuri obligatorii!\nDaca nu doriti sa le utilizati setati valori implicite pentru ele in Parametrizare Modul!",
                    "EROARE", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            } 
            #endregion


            #region Salvare unitate de masura compusa
            ///salvarea unitatii 
            DataSets.ESRPDataSetArticol.UnitatiMasuraRow umr = ((DataRowView)iDUnitateMasuraCB.SelectedItem).Row as DataSets.ESRPDataSetArticol.UnitatiMasuraRow;
            int pos = -1;
            pos = fkUMConversieBindingSource.Find("IDArticol", ar.IDArticol);
            if (umr.Compusa)
            {
                if (pos < 0)
                {
                    // adaugare
                    fkUMConversieBindingSource.AddNew();
                    DataSets.ESRPDataSetArticol.UMConversieRow umcr = ((DataRowView)fkUMConversieBindingSource.Current).Row as DataSets.ESRPDataSetArticol.UMConversieRow;
                    umcr.IDUMSursa = umr.IDUnitateMasura;
                    umcr.IDArticol = ar.IDArticol;
                    umcr.Factor = Convert.ToDouble(factorNUD.Value);
                    if (UMSimpleCB.SelectedItem != null)
                    {
                        umcr.IDUMDest = (((DataRowView)UMSimpleCB.SelectedItem).Row as DataSets.ESRPDataSetArticol.UnitatiMasuraRow).IDUnitateMasura;
                    }
                }
                else
                { //update 
                    fkUMConversieBindingSource.Position = pos;
                    DataSets.ESRPDataSetArticol.UMConversieRow umcr = ((DataRowView)fkUMConversieBindingSource.Current).Row as DataSets.ESRPDataSetArticol.UMConversieRow;
                    if (umcr.IDUMSursa != umr.IDUnitateMasura)
                        umcr.IDUMSursa = umr.IDUnitateMasura;
                    //umcr.IDArticol = ar.IDArticol;
                    if (umcr.Factor != Convert.ToDouble(factorNUD.Value))
                        umcr.Factor = Convert.ToDouble(factorNUD.Value);
                    if (UMSimpleCB.SelectedItem != null)
                    {
                        DataSets.ESRPDataSetArticol.UnitatiMasuraRow udmr = (((DataRowView)UMSimpleCB.SelectedItem).Row as DataSets.ESRPDataSetArticol.UnitatiMasuraRow);
                        if (umcr.IDUMDest != udmr.IDUnitateMasura)
                            umcr.IDUMDest = udmr.IDUnitateMasura;
                    }
                }
            }
            else
            {
                if (pos >= 0)
                {
                    fkUMConversieBindingSource.Position = pos;
                    fkUMConversieBindingSource.RemoveCurrent();
                }
            } 
            #endregion

            String cor = denumireTB.Text;
            if (cor.Trim() != denumireTB.Text)
                denumireTB.Text = cor.Trim();
            base.Salvare(this, args);
            if (EroareSalvare)
                return;
            //salvarea conversiei
            

        }

        private void iDUnitateMasuraCB_SelectedValueChanged(object sender, EventArgs e)
        {
            //pentru schimbare pe inregistrarea curenta
            articoleBindingSource_PositionChanged(sender, e);
        }

        private void articoleBindingSource_PositionChanged(object sender, EventArgs e)
        {
            if ((iDUnitateMasuraCB.SelectedItem == null) || (articoleBindingSource.Current == null))
                return;

            DataSets.ESRPDataSetArticol.UnitatiMasuraRow umr = ((DataRowView)iDUnitateMasuraCB.SelectedItem).Row as DataSets.ESRPDataSetArticol.UnitatiMasuraRow;
            DataSets.ESRPDataSetArticol.ArticoleRow ar = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;

            ContineL.Visible = umr.Compusa;
            factorNUD.Visible = umr.Compusa;
            UMSimpleCB.Visible = umr.Compusa;

            if (umr.Compusa)
            {
                int pos = -1;
                pos = fkUMConversieBindingSource.Find("IDArticol", ar.IDArticol);
                if (pos != -1)
                {
                    fkUMConversieBindingSource.Position = pos;
                    DataSets.ESRPDataSetArticol.UMConversieRow umcr = ((DataRowView)fkUMConversieBindingSource.Current).Row as DataSets.ESRPDataSetArticol.UMConversieRow;
                    if (factorNUD.Value != Convert.ToDecimal(umcr.Factor))
                        factorNUD.Value = Convert.ToDecimal(umcr.Factor);
                    if (Convert.ToInt32(UMSimpleCB.SelectedValue) != umcr.IDUMDest)
                        UMSimpleCB.SelectedValue = umcr.IDUMDest;
                }
            }

        }

        override public void Cauta(object sender, EventArgs e)
        {
            EnableMainSearchButton = false;
            ArticoleSearchFrm frm = new ArticoleSearchFrm(ModuleInfo, esrpDataSetArticol.Articole);
            frm.ShowDialog();
            EnableMainBrowseButton = true;
            EnableMainSearchButton = true;
        }

        private void contContCB_Validating(object sender, CancelEventArgs e)
        {

            ComboBox cb = sender as ComboBox;
            if (cb == null || (articoleBindingSource.Current == null))
                return;
            String denumire = cb.Text.Trim(), den = String.Empty;
            if (denumire == String.Empty)
                return;

            String contArticol = contContCB.Text;
            String contCheltuiala = contCheltuialaCB.Text;
            String contVenit = contVenitCB.Text;


            if (cb.SelectedIndex == -1)
            {
                ///daca nu ezista in lista de selectie
                ///
                using (SqlConnection con = DeschideConexiune(workConnString))
                {
                    SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM [Conturi] WHERE [Cont]='" + denumire + "' AND [CodUnitate]=" + ModuleInfo.CodUnitate.ToString(), con);
                    Object ret = cmd.ExecuteScalar();
                    DataSets.ESRPDataSetArticol.ArticoleRow rp = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;
                    //daca nu exista in baza de date contul introdus
                    if (Convert.ToInt16(ret) == 0 && rp!=null)
                    {
                        Type mModule = null;
                        FrmCard newFrm = RuleazaFrm("erpforms", "ERPForms.ContCardFrm", new object[] { ModuleInfo, true }, out mModule) as FrmCard;
                        if (newFrm == null)
                        {
                            MyTrace("PartenerFrm::contCB_Validating(object sender, CancelEventArgs e) - eroare rulare");
                            MessageBox.Show("Eroare Program, va rugam sa va adresati distribuitorului!");
                            return;
                        }

                        mModule.InvokeMember("contAdaugat", BindingFlags.SetProperty, null, newFrm, new object[] { denumire });
                        if(rp.RowState!= DataRowState.Detached)
                            mModule.InvokeMember("denCont", BindingFlags.SetProperty, null, newFrm, new object[] { rp.Denumire });

                        if (newFrm.ShowDialog() == DialogResult.Cancel || newFrm.currentRow == null)
                        {
                            e.Cancel = true;
                            return;
                        }

                        denumire = newFrm.currentRow.Row["cont"].ToString().ToUpper();
                        //den = newFrm.currentRow.Row["Denumire"].ToString();

                        if (newFrm.NoRecAdded > 0)
                        {
                            conturiTableAdapter.Connection = DeschideConexiune(workConnString);
                            conturiTableAdapter.Fill(esrpDataSetArticol.Conturi, ModuleInfo.CodUnitate);
                            InchideConexiune(conturiTableAdapter.Connection);
                        }



                    }
                    //il adaugam in lista de conturi
                    //verificam daca Exista in 
                    /*cmd.CommandText = "SELECT [Tipuri] FROM [ConturiSpeciale] WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND [Cont]='" +
                        denumire + "'";
                    Object rez = cmd.ExecuteScalar();
                    if (rez != null && rez != DBNull.Value)
                    {
                        //exista contul
                        cmd.CommandText = "UPDATE [ConturiSpeciale] SET [Tipuri]='" + rez + (cb.Name == "contClientCB" ? "{CLT}" : "{FRZ}") + "' WHERE [CodUnitate]=" + ModuleInfo.CodUnitate.ToString() + " AND [Cont]='" +
                            denumire + "'";
                    }
                    else
                    {
                        cmd.CommandText = "INSERT INTO [ConturiSpeciale]([Cont],[CodUnitate],[Tipuri],[Descriere]) VALUES ('" + denumire + "'," + ModuleInfo.CodUnitate.ToString() +
                            ",'" + (cb.Name == "contClientCB" ? "{CLT}" : "{FRZ}") + "','')";
                    }
                    cmd.ExecuteScalar();
                    //String cntSave = cb.Text;
                    if (cb == contContCB)
                        conturiBindingSource.Filter = GetContPartener(con, "FRZ");
                    if (cb == contCheltuialaCB)
                        conturiBindingSourceCheltuiala.Filter = GetContPartener(con, "CLT");
                    if (cb == contVenitCB)
                        conturiBindingSourceVenituri.Filter = GetContPartener(con, "CLT");
                    //cb.Text = cntSave;*/
                }

            }
            //incarca 

            //denContCB.Text = den;

            if (contArticol != String.Empty)
            {
                conturiBindingSource.Position = conturiBindingSource.Find("Cont", contArticol);
            }
            else
            {
                contContCB.SelectedIndex = -1;
                contDenumireCB.SelectedIndex = -1;
            }

            if (contCheltuiala != String.Empty)
            {

                conturiBindingSourceCheltuiala.Position = conturiBindingSourceCheltuiala.Find("Cont", contCheltuiala);
            }
            else
            {
                contCheltuialaCB.SelectedIndex = -1;
                denContCheltuialaTB.SelectedIndex = -1;
            }

            if (contVenit != String.Empty)
            {

                conturiBindingSourceVenituri.Position = conturiBindingSourceVenituri.Find("Cont", contVenit);
            }
            else
            {
                contVenitCB.SelectedIndex = -1;
                contVenitDenumireCB.SelectedIndex = -1;
            }
         
            /*ComboBox cb = sender as ComboBox;
            if (cb == null || articoleBindingSource.Current==null)
                return;
            String denumire = cb.Text.Trim(), den = String.Empty;
            if (denumire == String.Empty || (cb.SelectedIndex != -1))
                return;

            if (cb.SelectedIndex == -1)
            {
                Type mModule = null;
                FrmCard newFrm = RuleazaFrm("erpforms", "ERPForms.ContCardFrm", new object[] { ModuleInfo, true },out mModule) as FrmCard;
                if (newFrm == null)
                {
                    MyTrace("ArticoleCardFrm::contContCB_Validating - eroare rulare");
                    MessageBox.Show("Eroare Program, va rugam sa va adresati distribuitorului!");
                    return;
                }
                        
                DataSets.ESRPDataSetArticol.ArticoleRow ar = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;

                mModule.InvokeMember("contAdaugat", BindingFlags.SetProperty, null, newFrm, new object[] { denumire });
                mModule.InvokeMember("denCont", BindingFlags.SetProperty, null, newFrm, new object[] { ar.Denumire });

                if (newFrm.ShowDialog() == DialogResult.Cancel || newFrm.currentRow == null)
                {
                    e.Cancel = true;
                    return;
                }

                denumire = newFrm.currentRow.Row["cont"].ToString().ToUpper();
                den = newFrm.currentRow.Row["Denumire"].ToString();

                //incarca 
                SuspendLayout();
                tabControl1.SuspendLayout();
                conturiTP.SuspendLayout();

                String contArt = contContCB.Text;
                String contChelt = contCheltuialaCB.Text;
                String contVenit = contVenitCB.Text;
                //contContCB.Enabled = false; contVenitCB.Enabled = false; contVenitDenumireCB.Enabled = false;
                //contCheltuialaCB.Enabled = false; contDenumireCB.Enabled = false; denContCheltuialaTB.Enabled = false;
                Application.DoEvents();
                contContCB.BeginUpdate(); contVenitCB.BeginUpdate(); contVenitDenumireCB.BeginUpdate();
                contCheltuialaCB.BeginUpdate(); contDenumireCB.BeginUpdate(); denContCheltuialaTB.BeginUpdate();
                //contContCB. = false; contVenitCB.Enabled = false; contVenitDenumireCB.Enabled = false;
                //contCheltuialaCB.Enabled = false; contDenumireCB.Enabled = false; denContCheltuialaTB.Enabled = false;

                if (newFrm.NoRecAdded > 0)
                {
                    //conturiBindingSourceCheltuiala.SuspendBinding();
                    //conturiBindingSourceVenituri.SuspendBinding();
                    //conturiBindingSource.SuspendBinding();
                    Cursor.Current = Cursors.WaitCursor;
                    conturiTableAdapter.Connection = DeschideConexiune(workConnString);
                    conturiTableAdapter.Fill(esrpDataSetArticol.Conturi, ModuleInfo.CodUnitate);
                    InchideConexiune(conturiTableAdapter.Connection);
                    Cursor.Current = Cursors.Default;
                    //contCheltuialaCB.Refresh();
                    //conturiBindingSourceCheltuiala.ResumeBinding();
                    //conturiBindingSourceVenituri.ResumeBinding();
                    //conturiBindingSource.ResumeBinding();
                }

                cb.Text = denumire;
                //conturiBindingSource.ResetBindings();
                    
                if (cb == contContCB)
                {
                    contDenumireCB.Text = den;
                    int posc = conturiBindingSourceCheltuiala.Find("Cont", contChelt);
                    conturiBindingSourceCheltuiala.Position = posc;
                    //conturiBindingSourceCheltuiala.ResetCurrentItem();
                    //posc = conturiBindingSourceVenituri.Find("Cont", contVenit);
                    conturiBindingSourceVenituri.Position = posc;
                    conturiBindingSourceVenituri.ResetCurrentItem();
                        
                }

                if (cb == contCheltuialaCB)
                {
                    denContCheltuialaTB.Text = den;
                    int posc = 0;
  
                    posc = conturiBindingSource.Find("Cont", contArt);
                    conturiBindingSource.Position = posc;
                    posc = conturiBindingSourceVenituri.Find("Cont", contVenit);
                    conturiBindingSourceVenituri.Position = posc;
                       
                }

                if (cb == contVenitCB)
                {
                    contVenitDenumireCB.Text = den;
                    int posc = conturiBindingSourceCheltuiala.Find("Cont", contChelt);
                    conturiBindingSourceCheltuiala.Position = posc;
                    posc = conturiBindingSource.Find("Cont", contArt);
                    conturiBindingSource.Position = posc;

                }
                contContCB.EndUpdate(); contVenitCB.EndUpdate(); contVenitDenumireCB.EndUpdate();
                contCheltuialaCB.EndUpdate(); contDenumireCB.EndUpdate(); denContCheltuialaTB.EndUpdate();
                  
                conturiTP.ResumeLayout();
                tabControl1.ResumeLayout();
                ResumeLayout();
            }
            */
        }

        private void denumireTextBox_Validating(object sender, CancelEventArgs e)
        {
            denumireTB.Text = denumireTB.Text.Replace("/", " PE ").ToUpper().Trim();
            denumireTB.Text = denumireTB.Text.Replace("  ", " "); 
        }

        private void esteServiciuCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            StocuriDGV.Enabled = !esteServiciuCB.Checked;
        }

        private void StocuriDGV_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            DataGridView dg = sender as DataGridView;
            if (e.ListChangedType != ListChangedType.Reset || dg.Rows.Count <= 0)
                return;
            MyTrace(e.ListChangedType + "," + dg.Rows.Count.ToString());
            Decimal totalStoc = Decimal.Zero;

            DataSets.ESRPDataSetArticol.ArticoleRow artRow = ((DataRowView)articoleBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ArticoleRow;
            foreach (DataGridViewRow dc in dg.Rows)
            {
                Boolean haveRulaje = Convert.ToDecimal(dc.Cells[intrariColumn.Index].Value)!=Decimal.Zero
                    || Convert.ToDecimal(dc.Cells[iesiriColumn.Index].Value) != Decimal.Zero;

                dc.Cells[pretColumn.Index].ReadOnly = haveRulaje;
                dc.Cells[pretColumn.Index].Style.BackColor = haveRulaje ? Color.LightGray : dc.DefaultCellStyle.BackColor;
                dc.Cells[variantaColumn.Index].ReadOnly = haveRulaje;
                dc.Cells[variantaColumn.Index].Style.BackColor = haveRulaje ? Color.LightGray : dc.DefaultCellStyle.BackColor;
                //daca a fost setat modul de lucru fara variante si totusi stocul e cu variante
                /// il marcheaza in header
                if (!FolosesteVarianta)
                {
                    if (dc.Cells[variantaColumn.Index].Value != DBNull.Value && dc.Cells[variantaColumn.Index].Value != null)
                    {
                        dc.HeaderCell.Style.BackColor = Color.Purple;
                        //dc.Cells[0].Value = "V";
                    }
                }
                dc.Cells[stocColumn.Index].Value = Convert.ToDecimal(dc.Cells[stocInitialColumn.Index].Value)+Convert.ToDecimal(dc.Cells[stocPrecedentColumn.Index].Value) +
                Convert.ToDecimal(dc.Cells[intrariColumn.Index].Value)-Convert.ToDecimal(dc.Cells[iesiriColumn.Index].Value);
                if (Convert.ToDecimal(dc.Cells[stocColumn.Index].Value) < Decimal.Zero)
                    dc.Cells[stocColumn.Index].Style.ForeColor = Color.Red;
                else
                    dc.Cells[stocColumn.Index].Style.ForeColor = dc.DefaultCellStyle.ForeColor;

                if (dc.Cells[stocColumn.Index].Value != DBNull.Value)
                {
                    if (dc.Cells[stocColumn.Index].IsInEditMode)
                        totalStoc += Convert.ToDecimal(dc.Cells[stocColumn.Index].EditedFormattedValue);
                    else
                        totalStoc += Convert.ToDecimal(dc.Cells[stocColumn.Index].Value);
                }
            }
            if (totalTB.Value != totalStoc)
                totalTB.Value = totalStoc;
            double valnewSup = 0;
            if (!artRow.IsLungimeNull() && !artRow.IsLatimeNull())
            {
                valnewSup = ((artRow.Lungime * artRow.Latime) / 1000000.00) * Convert.ToDouble(totalStoc);
            }

            if (SupTotalNUD.Value != Convert.ToDecimal(valnewSup))
                SupTotalNUD.Value = Convert.ToDecimal(valnewSup);
 
        }

        private void StocuriDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dg = sender as DataGridView;

            if (dg.Rows[e.RowIndex].DataBoundItem == null)
                return;
            if (e.ColumnIndex == intrariColumn.Index)
            {
                DataSets.ESRPDataSetArticol.StocuriRow sr = ((DataRowView)dg.Rows[e.RowIndex].DataBoundItem).Row as DataSets.ESRPDataSetArticol.StocuriRow;
                if (sr != null)
                { 
                    DocumenteRezumatFrm frm = new DocumenteRezumatFrm(ModuleInfo, true, null, null,-1,false,-1,-1,sr.IDArticol,-1, sr.Pret,-1,GestiuneCantitativa);
                    frm.ShowDialog();
                }
            }

            if (e.ColumnIndex == iesiriColumn.Index)
            {
                DataSets.ESRPDataSetArticol.StocuriRow sr = ((DataRowView)dg.Rows[e.RowIndex].DataBoundItem).Row as DataSets.ESRPDataSetArticol.StocuriRow;
                if (sr != null)
                {
                    DocumenteRezumatFrm frm = new DocumenteRezumatFrm(ModuleInfo, false, null, null, -1, false, -1, -1, -1,sr.IDArticol,sr.Pret,-1,GestiuneCantitativa);
                    frm.ShowDialog();
                }
            }
        }
        
        private void StocuriDGV_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dg = sender as DataGridView;
            DataGridViewRow dc = dg.Rows[e.RowIndex];
            if (dc == null)
                return;
            dc.Cells[stocColumn.Index].Value = Convert.ToDecimal(dc.Cells[stocInitialColumn.Index].Value) + Convert.ToDecimal(dc.Cells[stocPrecedentColumn.Index].Value) +
                Convert.ToDecimal(dc.Cells[intrariColumn.Index].Value) - Convert.ToDecimal(dc.Cells[iesiriColumn.Index].Value);
            if (Convert.ToDecimal(dc.Cells[stocColumn.Index].Value) < Decimal.Zero)
                dc.Cells[stocColumn.Index].Style.ForeColor = Color.Red;
            else
                dc.Cells[stocColumn.Index].Style.ForeColor = dc.DefaultCellStyle.ForeColor;
        
        }

        private void articoleBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            SuprafataL.Text = ((lungimeNUD.Value * latimeNUD.Value) / (Decimal)1000000.00).ToString() + " mp";
        }

        private void lungimeNUD_ValueChanged(object sender, EventArgs e)
        {
            articoleBindingSource_CurrentChanged(sender,e);
        }

        private void latimeNUD_ValueChanged(object sender, EventArgs e)
        {
            articoleBindingSource_CurrentChanged(sender, e);
        }

        private Decimal GetSum(int indexColoana, DataGridView DGV)
        {

            Decimal total = Decimal.Zero;
            foreach (DataGridViewRow dr in DGV.Rows)
            {
                
            }
            return total;
        }

        private void productieOperatiuniBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            esrpDataSetArticol.ProductieOperatiuni.PozitieColumn.DefaultValue = productieOperatiuniBindingSource.Count + 1;
        }

        private void productieArticoleOperatiuniDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dg = sender as DataGridView;
            DataSets.ESRPDataSetArticol.ProductieArticoleOperatiuniRow sr = ((DataRowView)productieArticoleOperatiuniBindingSource.Current).Row as DataSets.ESRPDataSetArticol.ProductieArticoleOperatiuniRow;
            if (sr == null)
                return;
            if (dg.Rows[e.RowIndex].DataBoundItem != null && e.ColumnIndex == StocLinkColumn.Index)
            {
                StocuriFrm frm = new StocuriFrm(ModuleInfo, sr.IDArticol, DateTime.Now, productieArticoleOperatiuniDGV[denumireColumn.Index,e.RowIndex].FormattedValue.ToString(),-1);
                frm.ShowDialog();
            }
            if (dg.Rows[e.RowIndex].DataBoundItem != null && e.ColumnIndex == EchivalentColumn.Index)
            {

                StocuriProductieFrm frm = new StocuriProductieFrm(ModuleInfo/*, sr.IDArticol, DateTime.Now, productieArticoleOperatiuniDGV[denumireColumn.Index, e.RowIndex].FormattedValue.ToString()*/);
                frm.ShowDialog();

            }

            
        }

       
        
    }
}
